package org.cargloss.cp.controller;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.cargloss.cp.controller.modul.PageController;
import org.cargloss.cp.model.Banner;
import org.cargloss.cp.model.Event;
import org.cargloss.cp.model.Menus;
import org.cargloss.cp.model.News;
import org.cargloss.cp.model.OurBusiness;
import org.cargloss.cp.model.OurCustomer;
import org.cargloss.cp.model.StaticPage;
import org.cargloss.cp.model.WebTracking;

import util.hibernate.DAO;

import com.opensymphony.xwork2.ActionSupport;

public class ActionPage extends ActionSupport {
	/**
	 * 
	 */
	private String nav;
	private static final long serialVersionUID = 3107220959703009022L;
	private PageController pageControl = new PageController();
	private Map<String, String> application;
	// private List<Menus> menus;

	private List<Menus> listMenu;
	private List<Menus> listSubMenu;
	private List<Banner> listBanner;
	private List<News> listNews;
	private List<News> listNewsLimit;
	private List<OurBusiness> listBusiness;
	private List<OurCustomer> listCustomer;
	private List<Event> listEvent;
	private List<Event> listEventLimit;
	private long webCounter;
	private long webCounterToday;
	private long webCounterMonth;
	private int id;
	private StaticPage staticPage;
	
	private int start;
	private int limit;
	private boolean haveOlder;

	public boolean isHaveOlder() {
		return haveOlder;
	}

	public void setHaveOlder(boolean haveOlder) {
		this.haveOlder = haveOlder;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	private News news;
	private Event events;

	public Event getEvents() {
		return events;
	}

	public void setEvents(Event events) {
		this.events = events;
	}

	public News getNews() {
		return news;
	}

	public void setNews(News news) {
		this.news = news;
	}

	public StaticPage getStaticPage() {
		return staticPage;
	}

	public void setStaticPage(StaticPage staticPage) {
		this.staticPage = staticPage;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public long getWebCounter() {
		return webCounter;
	}

	public void setWebCounter(long webCounter) {
		this.webCounter = webCounter;
	}

	public List<OurCustomer> getListCustomer() {
		return listCustomer;
	}

	public List<Event> getListEvent() {
		return listEvent;
	}

	public void setListEvent(List<Event> listEvent) {
		this.listEvent = listEvent;
	}

	public void setListCustomer(List<OurCustomer> listCustomer) {
		this.listCustomer = listCustomer;
	}

	public String execute() {
		// pathSetting();
		application = new ApplicationSetting().getSetting();
		nav = "home";
		pageControl.setNavText("Banner");
		pageControl.setAll(false);
		pageControl.setHeader(true);
		pageControl.setFooter(true);
		pageControl.setContent(false);
		pageControl.setSubHeader(true);
		pageControl.setOurBusiness(true);
		pageControl.setOurCustomer(true);
		populateMenu();
		populateBanner();
		populateNews();
		populateBusiness();
		populateCustomer();
		populateEvent();
		ipTracking();
		webCounter();
		return SUCCESS;
	}

	@SuppressWarnings("unchecked")
	private void populateMenu() {
		DAO<Integer> dao = new DAO<Integer>();
		DAO<Integer> dao2 = new DAO<Integer>();
		dao.createCriteria(Menus.class);
		dao.addFilter("isNull", "parent.id", 0);
		dao.addOrder("asc", "id");
		dao.addOrder("asc", "parent");
		listMenu = (List<Menus>) dao.find();

		dao2.createCriteria(Menus.class);
		dao2.addFilter("gt", "parent.id", 0);
		dao2.addOrder("asc", "id");
		dao2.addOrder("asc", "parent");
		listSubMenu = (List<Menus>) dao2.find();

		dao.close();
	}

	private void defaultSetting() {
		application = new ApplicationSetting().getSetting();
		populateMenu();
		populateNews();
		populateEvent();
		ipTracking();
		webCounter();
		pageControl.setAll(false);
		pageControl.setHeader(true);
		pageControl.setFooter(true);
		pageControl.setContent(true);
	}

	public String staticpage() {
		defaultSetting();
		DAO<Integer> dao = new DAO<Integer>();
		dao.createCriteria(StaticPage.class);
		dao.addFilter("eq", "id", Integer.valueOf(getId()));
		staticPage = (StaticPage) dao.findUnique();
		dao.close();
		return SUCCESS;
	}

	public String news() {
		defaultSetting();
		DAO<Integer> dao = new DAO<Integer>();
		dao.createCriteria(News.class);
		dao.addFilter("eq", "id", Integer.valueOf(getId()));
		news = (News) dao.findUnique();
		dao.close();
		return SUCCESS;
	}

	@SuppressWarnings("unchecked")
	public String eventlist() {
		defaultSetting();
		DAO<Integer> dao = new DAO<Integer>();
		Long records = dao.count(Event.class);
		if (records < getLimit()) {
			haveOlder = false;

		} else {
			haveOlder = true;
		}
		dao.createCriteria(Event.class);
		listEventLimit = (List<Event>) dao.find(getStart(), getLimit());
		dao.close();
		return SUCCESS;
	}

	@SuppressWarnings("unchecked")
	public String newslist() {
		defaultSetting();

		DAO<Integer> dao = new DAO<Integer>();
		Long records = dao.count(News.class);
		if (records < getLimit()) {
			haveOlder = false;

		} else {
			haveOlder = true;
		}
		dao.createCriteria(News.class);
		listNewsLimit = (List<News>) dao.find(getStart(), getLimit());
		// listNewsLimit = (List<News>) dao.find(0, 10);
		dao.close();
		return SUCCESS;
	}

	public String event() {
		defaultSetting();
		DAO<Integer> dao = new DAO<Integer>();
		dao.createCriteria(Event.class);
		dao.addFilter("eq", "id", Integer.valueOf(getId()));
		events = (Event) dao.findUnique();
		//populateEventNotSelected();
		dao.close();
		return SUCCESS;
	}

	@SuppressWarnings("unchecked")
	private void populateBanner() {
		DAO<Integer> dao = new DAO<Integer>();
		dao.createCriteria(Banner.class);
		dao.addOrder("asc", "orderBy");
		setListBanner((List<Banner>) dao.find());
		dao.close();
	}

	@SuppressWarnings("unchecked")
	private void populateEvent() {
		DAO<Integer> dao = new DAO<Integer>();
		dao.createCriteria(Event.class);
		dao.addOrder("asc", "eventDateStart");
		setListEvent((List<Event>) dao.find(0,3));
		dao.close();
	}

	@SuppressWarnings("unchecked")
	private void populateEventNotSelected() {
		DAO<Integer> dao = new DAO<Integer>();
		dao.createCriteria(Event.class);
		dao.addFilter("eq", "id", getId());
		dao.addOrder("desc", "id");
		setListEvent((List<Event>) dao.find());
		dao.close();
	}

	@SuppressWarnings("unchecked")
	private void populateNews() {
		DAO<Integer> dao = new DAO<Integer>();
		dao.createCriteria(News.class);
		dao.addOrder("desc", "id");
		setListNews((List<News>) dao.find(0, 3));
		dao.close();
	}

	@SuppressWarnings("unchecked")
	private void populateBusiness() {
		DAO<Integer> dao = new DAO<Integer>();
		dao.createCriteria(OurBusiness.class);
		dao.addOrder("desc", "id");
		setListBusiness((List<OurBusiness>) dao.find(0, 5));
		dao.close();
	}

	private void ipTracking() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String IP = request.getRemoteAddr();
		DAO<Integer> dao = new DAO<Integer>();
		dao.createCriteria(WebTracking.class);
		dao.addFilter("eq", "ip", IP);
		// System.out.println(dao.find().size());
		if (dao.find().size() <= 0) {
			Calendar calendar = Calendar.getInstance();
			Date now = calendar.getTime();
			WebTracking wt = new WebTracking();
			wt.setIp(IP);
			wt.setAccessDate(now);
			dao.insert(wt);
			// webCounter = dao.count(WebTracking.class);
		}
		dao.close();
	}

	private void webCounter() {
		DAO<Integer> dao = new DAO<Integer>();
		
		Calendar from = Calendar.getInstance();
		dao.createQuery("from WebTracking r WHERE day(r.accessDate) =:dateFrom and month(accessDate) =:bulan and year(accessDate) =:tahun");
		dao.queryParamter("dateFrom", from.get(Calendar.DATE));
		dao.queryParamter("bulan", from.get(Calendar.MONTH) + 1);
		dao.queryParamter("tahun", from.get(Calendar.YEAR));
		
		webCounter = dao.resultQuery().size();
		webCounterToday = dao.resultQuery().size();

		dao.createQuery("from WebTracking r WHERE month(accessDate) =:bulan and year(accessDate) =:tahun");
		
		dao.queryParamter("bulan", from.get(Calendar.MONTH) +1);
		dao.queryParamter("tahun", from.get(Calendar.YEAR));
		webCounterMonth = dao.resultQuery().size();
		System.out.println(from.get(Calendar.YEAR) + " " + from.getTime().getMonth() + " " + from.getTime().getDate());
		dao.close();
	}

	@SuppressWarnings("unchecked")
	private void populateCustomer() {
		DAO<Integer> dao = new DAO<Integer>();
		dao.createCriteria(OurCustomer.class);
		dao.addOrder("desc", "id");
		setListCustomer((List<OurCustomer>) dao.find(0, 5));
		dao.close();
	}

	public Map<String, String> getApplication() {
		return application;
	}

	public void setApplication(Map<String, String> application) {
		this.application = application;
	}

	public PageController getPageControl() {
		return pageControl;
	}

	public void setPageControl(PageController pageControl) {
		this.pageControl = pageControl;
	}

	public String login() {
		nav = "login";
		return SUCCESS;
	}

	public String getNav() {
		return nav;
	}

	public List<Menus> getListMenu() {
		return listMenu;
	}

	public void setListMenu(List<Menus> listMenu) {
		this.listMenu = listMenu;
	}

	public void setNav(String nav) {
		this.nav = nav;
	}

	public List<Menus> getListSubMenu() {
		return listSubMenu;
	}

	public void setListSubMenu(List<Menus> listSubMenu) {
		this.listSubMenu = listSubMenu;
	}

	public List<Banner> getListBanner() {
		return listBanner;
	}

	public void setListBanner(List<Banner> listBanner) {
		this.listBanner = listBanner;
	}

	public List<News> getListNews() {
		return listNews;
	}

	public void setListNews(List<News> listNews) {
		this.listNews = listNews;
	}

	public List<OurBusiness> getListBusiness() {
		return listBusiness;
	}

	public void setListBusiness(List<OurBusiness> listBusiness) {
		this.listBusiness = listBusiness;
	}

	public List<Event> getListEventLimit() {
		return listEventLimit;
	}

	public void setListEventLimit(List<Event> listEventLimit) {
		this.listEventLimit = listEventLimit;
	}

	public List<News> getListNewsLimit() {
		return listNewsLimit;
	}

	public void setListNewsLimit(List<News> listNewsLimit) {
		this.listNewsLimit = listNewsLimit;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public long getWebCounterToday() {
		return webCounterToday;
	}

	public void setWebCounterToday(long webCounterToday) {
		this.webCounterToday = webCounterToday;
	}

	public long getWebCounterMonth() {
		return webCounterMonth;
	}

	public void setWebCounterMonth(long webCounterMonth) {
		this.webCounterMonth = webCounterMonth;
	}

}
